package com.yanqu.road.server.manger.activity.snowgarden;

import com.yanqu.road.dao.impl.activity.snowgarden.SnowGardenUserDataDaoImpl;
import com.yanqu.road.dao.impl.activity.snowgarden.UserSnowGardenVisitRecordDaoImpl;
import com.yanqu.road.entity.activity.snowgarden.SnowGardenUserData;
import com.yanqu.road.entity.activity.snowgarden.UserSnowGardenVisitRecord;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.snowgarden.SnowGardenModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CacheSnowDataActivity {
    private int activityId;
    //离线缓存的数据,等待入库或者玩家上线后取过去更新,key=userId
    private Map<Long, SnowGardenUserData> userDataMap = new ConcurrentHashMap<>();
    private Map<Long, List<UserSnowGardenVisitRecord>> cacheUserVisitRecordDataMap = new ConcurrentHashMap<>();

    public CacheSnowDataActivity(int activityId) {
        this.activityId = activityId;
        loadDb();
    }
    
    public void loadDb(){
        Map<Long, SnowGardenUserData> tempMap = new SnowGardenUserDataDaoImpl().getSnowGardenUserDataMap(activityId);
        userDataMap = tempMap;
    }
    
    public void save() {
        List<SnowGardenUserData> needSaveUserDataList = new ArrayList<>();
        synchronized (userDataMap) {
            needSaveUserDataList.addAll(userDataMap.values());
        }

        for (SnowGardenUserData data : needSaveUserDataList) {
            if (data.isInsertOption()) {
                new SnowGardenUserDataDaoImpl().add(data);
            } else if (data.isUpdateOption()) {
                new SnowGardenUserDataDaoImpl().update(data);
            }
        }

        List<UserSnowGardenVisitRecord> needSaveUserVisitRecordDataList = new ArrayList<>();
        synchronized (cacheUserVisitRecordDataMap) {
            for (List<UserSnowGardenVisitRecord> visitRecords : cacheUserVisitRecordDataMap.values()) {
                needSaveUserVisitRecordDataList.addAll(visitRecords);
            }
            cacheUserVisitRecordDataMap.clear();
        }

        for (UserSnowGardenVisitRecord visitRecord : needSaveUserVisitRecordDataList) {
            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(visitRecord.getUserId());
            if (onlinePlayer != null) {
                onlinePlayer.getModule(SnowGardenModule.class).addVisitRecord(visitRecord.getActivityId(), visitRecord.getTargetUserId(), visitRecord.getVisitType(), visitRecord.getObjectId(), visitRecord.getAddScore(), false, visitRecord.getVisitTime());
            } else {
                if (visitRecord.isInsertOption()) {
                    new UserSnowGardenVisitRecordDaoImpl().add(visitRecord);
                } else if (visitRecord.isUpdateOption()) {
                    new UserSnowGardenVisitRecordDaoImpl().update(visitRecord);
                }
            }

        }
    }

    public SnowGardenUserData getSnowGardenUserDataNewIfNull(int activityId, long userId) {
        if (userDataMap.containsKey(userId)) {
            return userDataMap.get(userId);
        }
        synchronized (userDataMap){
            if (userDataMap.containsKey(userId)) {
                return userDataMap.get(userId);
            }
            SnowGardenUserData userData = SnowGardenMgr.createUserData(userId);
            userDataMap.put(userId, userData);
        }

        return userDataMap.get(userId);
    }

    public SnowGardenUserData getSnowGardenUserDataOnlyGet(long userId) {
        return userDataMap.get(userId);
    }

    public void addCacheVisitRecord(int activityId, long ownUserId, long targetUserId, int visitType, int objectId, int addScore, int nowPeriod) {
        long currentTime = DateHelper.getCurrentTime();
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(ownUserId);
        if (onlinePlayer != null) {
            onlinePlayer.getModule(SnowGardenModule.class).addVisitRecord(activityId, targetUserId, visitType, objectId, addScore, false, currentTime);
        } else {
            UserSnowGardenVisitRecord visitRecord = new UserSnowGardenVisitRecord(activityId, ownUserId, nowPeriod, targetUserId, visitType, objectId, addScore, 1, currentTime);
            synchronized (cacheUserVisitRecordDataMap) {
                List<UserSnowGardenVisitRecord> list = cacheUserVisitRecordDataMap.get(ownUserId);
                if (list == null) {
                    list = new ArrayList<>();
                    cacheUserVisitRecordDataMap.put(ownUserId, list);
                }
                list.add(visitRecord);
            }
        }
    }

    public List<UserSnowGardenVisitRecord> popCacheVisitRecordData(long userId) {
        synchronized (cacheUserVisitRecordDataMap) {
            return cacheUserVisitRecordDataMap.remove(userId);
        }
    }
}
